package com.ruoyi.web.controller.platform;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.db.DSqlKit;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宫颈癌病史Controller
 *
 * @author ruoyi
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/platform/rxa")
public class rxaController extends BaseController
{


    /**
     * 查询乳腺癌病史列表
     */

    @GetMapping("/bslist")
    public TableDataInfo bslist(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        System.out.println(loginUser.getUser().getEmail());
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        String select="select   PE_ID,  PE_VISIT_ID,  ID_NO,  P_ID,  LAST_MENSTRUAL_PERIOD ,  CHECK_BREAST_CANCER,  EXAMINATION_SCOPE,  BH,  AUTO_VALUE,\n" +
                "  SAVE_DATE,  SAVE_PERSON,  SAVE_FILE ,  UPLOAD,  UPLOADRESULT,  YEAR_REPORTED,  QUARTERLY_REPORTED ,dept_result,result,create_time";
        DSqlKit.init(" from rxabs  where 1=1 ");

        //上传状态
        if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
        {
            DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
        {
            DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
        }




        if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
        {

            DSqlKit.append(" and (create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
        }
        if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
        {

            DSqlKit.append(" and (SAVE_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
        }

        if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
        {
            String val = "%" +request.getParameter("PE_ID").toString()+ "%";
            DSqlKit.append(" and (PE_ID like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
        {
            DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
        {
            String val = "%" +request.getParameter("ID_NO").toString()+ "%";
            DSqlKit.append(" and (ID_NO like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("BH")))
        {
            String val = "%" +request.getParameter("BH").toString()+ "%";
            DSqlKit.append(" and (BH like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
            DSqlKit.append(" and (UPLOAD like ?) ", val);
        }
        if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
                && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            DSqlKit.append(" and ((SAVE_FILE = '0') or (SAVE_DATE>  CURDATE()-1))");
        }

        DSqlKit.append(" order by  save_DATE desc ");
        Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
        String[] tmp={"CHECK_BREAST_CANCER","TRICHOMONAS_VAGINITIS","VULVOVAGINAL_CANDIDIASIS",
        "BACTERIAL_VAGINOSIS","MUCOPURULENT_CERVICITIS","CERVICAL_POLYP","FIBROID","OTHER"};
        for(Record data:page.getList())
        {
            for(String tmpStr:tmp)
            {
                if(data.get(tmpStr)!=null)
                {
                    if(data.getStr(tmpStr).equals("1"))
                    {
                        data.set(tmpStr,true);
                    }
                    else
                    {
                        data.set(tmpStr,false);
                    }
                }
                else
                {
                    data.set(tmpStr,false);
                }
            }
        }
        return getDataTable(page);
    }

    @ResponseBody
    @Log(title = "乳腺癌病史审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/bssave")
    public AjaxResult bssave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=bssaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }


    @ResponseBody
    @Log(title = "乳腺癌病史审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=bssaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=bssaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }
    public boolean bssaveData(Map<String,Object> data,String userName)
    {
        Record saverxabsData=new Record();
        saverxabsData.set("PE_ID",data.get("peId"));
        saverxabsData.set("PE_VISIT_ID",data.get("peVisitId"));
//        saverxabsData.set("ID_NO",data.get("idNo"));
        if(data.get("lastMenstrualPeriod")!=null)
        {
            saverxabsData.set("LAST_MENSTRUAL_PERIOD",data.get("lastMenstrualPeriod").toString().substring(0,10));
        }
        saverxabsData.set("CHECK_BREAST_CANCER",data.get("checkBreastCancer"));
        saverxabsData.set("EXAMINATION_SCOPE",data.get("examinationScope"));
        saverxabsData.set("SAVE_DATE",new Date());
        saverxabsData.set("SAVE_PERSON",userName);
        saverxabsData.set("SAVE_FILE","1");



        Boolean flag=Db.tx(() -> {
            Db.update("rxabs","PE_ID,PE_VISIT_ID",saverxabsData);

            return true;
        });
        return flag;
    }


    /********************************************************************************************************************************************

     * 查询乳腺癌触诊列表
     */
    @GetMapping("/czlist")
    public TableDataInfo czlist(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        System.out.println(loginUser);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        String select="select PE_ID ,  PE_VISIT_ID ,  CLINICAL_EXAMINATION_RESULT,  PLEASE_SPECIFY,  CLINICAL_EXAMINATION_RESULT2,  PLEASE_SPECIFY2, \n" +
                "CHECK_PERSON,  BH,  AUTO_VALUE,  SAVE_DATE,  SAVE_PERSON,  SAVE_FILE,  UPLOAD ,  UPLOADRESULT,  ID_NO,  P_ID ,dept_result,result,create_time";
        DSqlKit.init(" from rxgczxx  where 1=1 ");

        //上传状态
        if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
        {
            DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
        {
            DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
        }

        if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
        {

            DSqlKit.append(" and (create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
        }
        if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
        {

            DSqlKit.append(" and (SAVE_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
        }

        if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
        {
            String val = "%" +request.getParameter("PE_ID").toString()+ "%";
            DSqlKit.append(" and (PE_ID like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
        {
            DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
        {
            String val = "%" +request.getParameter("ID_NO").toString()+ "%";
            DSqlKit.append(" and (ID_NO like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("BH")))
        {
            String val = "%" +request.getParameter("BH").toString()+ "%";
            DSqlKit.append(" and (BH like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
            DSqlKit.append(" and (UPLOAD like ?) ", val);
        }
        if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
                && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            DSqlKit.append(" and ((SAVE_FILE = '0') or (SAVE_DATE>  CURDATE()-1)) ");
        }

        DSqlKit.append(" order by  save_DATE desc ");
        Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());

        return getDataTable(page);
    }

    @ResponseBody
    @Log(title = "乳腺癌触诊审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/czsave")
    public AjaxResult czsave(@RequestBody(required = true) Map<String,Object> data){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=czsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }
    }


    @ResponseBody
    @Log(title = "乳腺癌病史审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/czbatchSave")
    public AjaxResult czbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=czsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=czsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }
    public boolean czsaveData(Map<String,Object> data,String userName)
    {
        Record saverxabsData=new Record();
        saverxabsData.set("PE_ID",data.get("peId"));
        saverxabsData.set("PE_VISIT_ID",data.get("peVisitId"));
//        saverxabsData.set("ID_NO",data.get("idNo"));

        saverxabsData.set("CLINICAL_EXAMINATION_RESULT",data.get("clinicalExaminationResult"));
        if(data.get("pleaseSpecify")!=null)
        {
            saverxabsData.set("PLEASE_SPECIFY",data.get("pleaseSpecify").toString());
        }

        saverxabsData.set("CLINICAL_EXAMINATION_RESULT2",data.get("clinicalExaminationResult2"));
        if(data.get("pleaseSpecify2")!=null)
        {
            saverxabsData.set("PLEASE_SPECIFY2",data.get("pleaseSpecify2").toString());
        }
        saverxabsData.set("SAVE_DATE",new Date());
        saverxabsData.set("SAVE_PERSON",userName);
        saverxabsData.set("SAVE_FILE","1");



        Boolean flag=Db.tx(() -> {
            Db.update("rxgczxx","PE_ID,PE_VISIT_ID",saverxabsData);

            return true;
        });
        return flag;
    }

    /********************************************************************************************************************************************
     * 查询乳腺癌B超列表
     */
    @GetMapping("/bclist")
    public TableDataInfo bclist(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        System.out.println(loginUser);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        String select=" select  PE_ID,  PE_VISIT_ID,  SIZE1,  SIZE2,  SIZE3,  YSIZE1,  YSIZE2,\n" +
                "  YSIZE3,  CYST,  SOLID_MASS,  FORM,  DIRECTION,  BOUNDARY,  EDGE,  INTERNAL_ECHO,  POSTERIOR_ECHO,\n" +
                "  CALCIFICATION,  BLOOD_FLOW,  OTHERS,  YCYST,  YSOLID_MASS,  YFORM,  YDIRECTION,  YBOUNDARY,  YEDGE,\n" +
                "  YINTERNAL_ECHO,  YPOSTERIOR_ECHO,  YCALCIFICATION,  YBLOOD_FLOW,  YOTHERS,  SUGGESTION,  CLASSIFICATION,\n" +
                "  YCLASSIFICATION,  POSITION,  YPOSITION,  CHECK_PERSON,  CHECK_DATE,  ID_NO,  P_ID,  AUTO_VALUE,  UPLOAD,\n" +
                "  UPLOADRESULT,  SAVE_DATE ,  SAVE_PERSON,  SAVE_FILE ,dept_result,result,create_time";
        DSqlKit.init(" from cscsjcxx  where 1=1 ");

        //上传状态
        if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
        {
            DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
        {
            DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
        }



        if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
        {

            DSqlKit.append(" and (create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
        }
        if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
        {

            DSqlKit.append(" and (SAVE_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
        }

        if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
        {
            String val = "%" +request.getParameter("PE_ID").toString()+ "%";
            DSqlKit.append(" and (PE_ID like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
        {
            DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
        {
            String val = "%" +request.getParameter("ID_NO").toString()+ "%";
            DSqlKit.append(" and (ID_NO like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("BH")))
        {
            String val = "%" +request.getParameter("BH").toString()+ "%";
            DSqlKit.append(" and (BH like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
            DSqlKit.append(" and (UPLOAD like ?) ", val);
        }
        if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
                && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            DSqlKit.append(" and ((SAVE_FILE = '0') or (SAVE_DATE>  CURDATE()-1)) ");
        }

        DSqlKit.append(" order by  SAVE_DATE desc ");
        Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
        System.out.println(page);
        return getDataTable(page);
    }

    @ResponseBody
    @Log(title = "乳腺癌B超审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/bcsave")
    public AjaxResult bcsave(@RequestBody(required = true) Map<String,Object> data){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println(data);
        Boolean flag=bcsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }
    }


    @ResponseBody
    @Log(title = "乳腺癌病史审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/bcbatchSave")
    public AjaxResult bcbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=bcsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=bcsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }
    public boolean bcsaveData(Map<String,Object> data,String userName)
    {
        Record saverxabsData=new Record();
        saverxabsData.set("PE_ID",data.get("peId"));
        saverxabsData.set("PE_VISIT_ID",data.get("peVisitId"));
//        saverxabsData.set("ID_NO",data.get("idNo"));

        saverxabsData.set("SUGGESTION",data.get("suggestion"));
        saverxabsData.set("CLASSIFICATION",data.get("classification"));
        saverxabsData.set("YCLASSIFICATION",data.get("yclassification"));
        //****************************左乳
        if(data.get("cyst")!=null)        {            saverxabsData.set("CYST",data.get("cyst").toString());        }
        if(data.get("solidMass")!=null)        {            saverxabsData.set("SOLID_MASS",data.get("solidMass").toString());        }
        if(data.get("position")!=null)        {            saverxabsData.set("POSITION",data.get("position").toString());        }
        if(data.get("size1")!=null)        {            saverxabsData.set("SIZE1",data.get("size1").toString());        }
        if(data.get("size2")!=null)        {            saverxabsData.set("SIZE2",data.get("size2").toString());        }
        if(data.get("size3")!=null)        {            saverxabsData.set("SIZE3",data.get("size3").toString());        }
        if(data.get("form")!=null)        {            saverxabsData.set("FORM",data.get("form").toString());        }
        if(data.get("direction")!=null)        {            saverxabsData.set("DIRECTION",data.get("direction").toString());        }
        if(data.get("boundary")!=null)        {            saverxabsData.set("BOUNDARY",data.get("boundary").toString());        }
        if(data.get("edge")!=null)        {            saverxabsData.set("EDGE",data.get("edge").toString());        }
        if(data.get("internalEcho")!=null)        {            saverxabsData.set("INTERNAL_ECHO",data.get("internalEcho").toString());        }
        if(data.get("posteriorEcho")!=null)        {            saverxabsData.set("POSTERIOR_ECHO",data.get("posteriorEcho").toString());        }
        if(data.get("calcification")!=null)        {            saverxabsData.set("CALCIFICATION",data.get("calcification").toString());        }
        if(data.get("bloodFlow")!=null)        {            saverxabsData.set("BLOOD_FLOW",data.get("bloodFlow").toString());        }
        if(data.get("others")!=null)        {            saverxabsData.set("OTHERS",data.get("others").toString());        }

//****************************d右乳
        if(data.get("ycyst")!=null)        {            saverxabsData.set("YCYST",data.get("ycyst").toString());        }
        if(data.get("ysolidMass")!=null)        {            saverxabsData.set("YSOLID_MASS",data.get("ysolidMass").toString());        }
        if(data.get("yposition")!=null)        {            saverxabsData.set("YPOSITION",data.get("yposition").toString());        }
        if(data.get("ysize1")!=null)        {            saverxabsData.set("YSIZE1",data.get("ysize1").toString());        }
        if(data.get("ysize2")!=null)        {            saverxabsData.set("YSIZE2",data.get("ysize2").toString());        }
        if(data.get("ysize3")!=null)        {            saverxabsData.set("YSIZE3",data.get("ysize3").toString());        }
        if(data.get("yform")!=null)        {            saverxabsData.set("YFORM",data.get("yform").toString());        }
        if(data.get("ydirection")!=null)        {            saverxabsData.set("YDIRECTION",data.get("ydirection").toString());        }
        if(data.get("yboundary")!=null)        {            saverxabsData.set("YBOUNDARY",data.get("yboundary").toString());        }
        if(data.get("yedge")!=null)        {            saverxabsData.set("YEDGE",data.get("yedge").toString());        }
        if(data.get("yinternalEcho")!=null)        {            saverxabsData.set("YINTERNAL_ECHO",data.get("yinternalEcho").toString());        }
        if(data.get("yposteriorEcho")!=null)        {            saverxabsData.set("YPOSTERIOR_ECHO",data.get("yposteriorEcho").toString());        }
        if(data.get("ycalcification")!=null)        {            saverxabsData.set("YCALCIFICATION",data.get("ycalcification").toString());        }
        if(data.get("ybloodFlow")!=null)        {            saverxabsData.set("YBLOOD_FLOW",data.get("ybloodFlow").toString());        }
        if(data.get("yothers")!=null)        {            saverxabsData.set("YOTHERS",data.get("yothers").toString());        }

        saverxabsData.set("SAVE_DATE",new Date());
        saverxabsData.set("SAVE_PERSON",userName);
        saverxabsData.set("SAVE_FILE","1");



        Boolean flag=Db.tx(() -> {
            Db.update("cscsjcxx","PE_ID,PE_VISIT_ID",saverxabsData);

            return true;
        });
        return flag;
    }

    /********************************************************************************************************************************************

     * 查询乳腺癌X线列表
     */
    @GetMapping("/xxlist")
    public TableDataInfo xxlist(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        System.out.println(loginUser);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        String select=" select PE_ID ,  PE_VISIT_ID,  ID_NO,  P_ID,  CLASSIFICATION,  TUMOR,  SIZE1,  SIZE2,  SUSPECTED_CALCIFICATION,\n" +
                "  DISORGANIZATION,  ON_THE_OUTSIDE,  UNDER_THE,  ON_THE_INSIDE,  IN_THE,  THE_CENTRAL_AREA,  POSTERIOR_AREOLA ,\n" +
                "  OTHER ,  YCLASSIFICATION,  YTUMOR,  YSIZE1,  YSIZE2,  YSUSPECTED_CALCIFICATION,  YDISORGANIZATION,  YON_THE_OUTSIDE,\n" +
                "  YUNDER_THE,  YON_THE_INSIDE,  YIN_THE,  YTHE_CENTRAL_AREA,  YPOSTERIOR_AREOLA,  YOTHER,  SUGGESTION,  AUTO_VALUE,\n" +
                "  SAVE_DATE,  SAVE_PERSON,  SAVE_FILE,  UPLOAD,  UPLOADRESULT,  CHECK_DATE,  CHECK_PERSON,dept_result,result,create_time ";
        DSqlKit.init(" from rxaxx  where 1=1 ");
//上传状态
        if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
        {
            DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
        {
            DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
        }

        if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
        {

            DSqlKit.append(" and (create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
        }
        if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
        {

            DSqlKit.append(" and (SAVE_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
        }

        if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
        {
            String val = "%" +request.getParameter("PE_ID").toString()+ "%";
            DSqlKit.append(" and (PE_ID like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
        {
            DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
        {
            String val = "%" +request.getParameter("ID_NO").toString()+ "%";
            DSqlKit.append(" and (ID_NO like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("BH")))
        {
            String val = "%" +request.getParameter("BH").toString()+ "%";
            DSqlKit.append(" and (BH like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
            DSqlKit.append(" and (UPLOAD like ?) ", val);
        }
        if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
                && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            DSqlKit.append(" and ((SAVE_FILE = '0') or (SAVE_DATE>  CURDATE()-1)) ");
        }


        DSqlKit.append(" order by  save_DATE desc ");
        Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());

        return getDataTable(page);
    }

    @ResponseBody
    @Log(title = "乳腺癌XX审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/xxsave")
    public AjaxResult xxsave(@RequestBody(required = true) Map<String,Object> data){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println(data);
        Boolean flag=xxsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }
    }


    @ResponseBody
    @Log(title = "乳腺癌XX审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/xxbatchSave")
    public AjaxResult xxbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=xxsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=xxsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }
    public boolean xxsaveData(Map<String,Object> data,String userName) {
        Record saverxabsData = new Record();
        saverxabsData.set("PE_ID", data.get("peId"));
        saverxabsData.set("PE_VISIT_ID", data.get("peVisitId"));
//        saverxabsData.set("ID_NO",data.get("idNo"));

        saverxabsData.set("SUGGESTION", data.get("suggestion"));
        saverxabsData.set("CLASSIFICATION", data.get("classification"));
        saverxabsData.set("YCLASSIFICATION", data.get("yclassification"));
        //****************************左乳
        if (data.get("tumor") != null) {
            if(data.get("tumor").toString()=="true"){  saverxabsData.set("TUMOR","1");}
            else{ saverxabsData.set("TUMOR","0")   ;                }
        }
        if (data.get("suspectedCalcification") != null) {
//            saverxabsData.set("SUSPECTED_CALCIFICATION", data.get("suspectedCalcification").toString());
            if(data.get("suspectedCalcification").toString()=="true"){  saverxabsData.set("SUSPECTED_CALCIFICATION","1");}
            else{ saverxabsData.set("SUSPECTED_CALCIFICATION","0")   ;            }
        }
        if (data.get("disorganization") != null) {
//            saverxabsData.set("DISORGANIZATION", data.get("disorganization").toString());
            if(data.get("disorganization").toString()=="true"){  saverxabsData.set("DISORGANIZATION","1");}
            else{ saverxabsData.set("DISORGANIZATION","0")   ;}
        }
//        if(data.get("size1")!=null)        {            saverxabsData.set("SIZE1",data.get("size1").toString());        }
//        if(data.get("size2")!=null)        {            saverxabsData.set("SIZE2",data.get("size2").toString());        }
//
        if (data.get("onTheOutside") != null) {
            saverxabsData.set("ON_THE_OUTSIDE", data.get("onTheOutside").toString());
        }
        if (data.get("underThe") != null) {
            saverxabsData.set("UNDER_THE", data.get("underThe").toString());
        }
        if (data.get("onTheInside") != null) {
            saverxabsData.set("ON_THE_INSIDE", data.get("onTheInside").toString());
        }
        if (data.get("inThe") != null) {
            saverxabsData.set("IN_THE", data.get("inThe").toString());
        }
        if (data.get("theCentralArea") != null) {
            saverxabsData.set("THE_CENTRAL_AREA", data.get("theCentralArea").toString());
        }
        if (data.get("posteriorAreola") != null) {
            saverxabsData.set("POSTERIOR_AREOLA", data.get("posteriorAreola").toString());
        }
        if (data.get("other") != null) {
            saverxabsData.set("OTHER", data.get("other").toString());
        }

//****************************d右乳
        if (data.get("ytumor") != null) {
//            saverxabsData.set("YTUMOR", data.get("ytumor").toString());
            if(data.get("ytumor").toString()=="true"){  saverxabsData.set("YTUMOR","1");}
            else{ saverxabsData.set("YTUMOR","0")   ;}
        }

        if (data.get("ysuspectedCalcification") != null) {
//            saverxabsData.set("YSUSPECTED_CALCIFICATION", data.get("ysuspectedCalcification").toString());
            if(data.get("ysuspectedCalcification").toString()=="true"){  saverxabsData.set("YSUSPECTED_CALCIFICATION","1");}
            else{ saverxabsData.set("YSUSPECTED_CALCIFICATION","0")   ;}
        }
        if (data.get("ydisorganization") != null) {
//            saverxabsData.set("YDISORGANIZATION", data.get("ydisorganization").toString());
            if(data.get("ydisorganization").toString()=="true"){  saverxabsData.set("YDISORGANIZATION","1");}
            else{ saverxabsData.set("YDISORGANIZATION","0")   ;}
        }
//        if(data.get("size1")!=null)        {            saverxabsData.set("SIZE1",data.get("size1").toString());        }
//        if(data.get("size2")!=null)        {            saverxabsData.set("SIZE2",data.get("size2").toString());        }
//
        if (data.get("yonTheOutside") != null) {
            saverxabsData.set("YON_THE_OUTSIDE", data.get("yonTheOutside").toString());
        }
        if (data.get("yunderThe") != null) {
            saverxabsData.set("YUNDER_THE", data.get("yunderThe").toString());
        }
        if (data.get("yonTheInside") != null) {
            saverxabsData.set("YON_THE_INSIDE", data.get("yonTheInside").toString());
        }
        if (data.get("yinThe") != null) {
            saverxabsData.set("YIN_THE", data.get("yinThe").toString());
        }
        if (data.get("ytheCentralArea") != null) {
            saverxabsData.set("YTHE_CENTRAL_AREA", data.get("ytheCentralArea").toString());
        }
        if (data.get("yposteriorAreola") != null) {
            saverxabsData.set("YPOSTERIOR_AREOLA", data.get("yposteriorAreola").toString());
        }
        if (data.get("yother") != null) {
            saverxabsData.set("YOTHER", data.get("yother").toString());
        }

        saverxabsData.set("SAVE_DATE", new Date());
        saverxabsData.set("SAVE_PERSON", userName);
        saverxabsData.set("SAVE_FILE", "1");

//        System.out.println(saverxabsData);
        Boolean flag = Db.tx(() -> {
            Db.update("rxaxx", "PE_ID,PE_VISIT_ID", saverxabsData);

            return true;
        });
        return flag;
    }

    @Log(title = "上传前校验各表要上传完成", businessType = BusinessType.UPDATE)
    @GetMapping ("/rxatest")
    public AjaxResult rxatest(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";

        Record bsData=Db.findFirst("SELECT * FROM rxabs where save_file<>'1'");
        if(bsData!=null) {
            Msg+="乳腺癌病史有未审核数据";
            return AjaxResult.error(Msg);
        }

        Record hpvData=Db.findFirst("SELECT * FROM rxgczxx where save_file<>'1'");
        if(hpvData!=null) {
            Msg+="乳腺癌解诊有未审核数据";
            return AjaxResult.error(Msg);
        }
        Record xbxData=Db.findFirst("SELECT * FROM cscsjcxx where save_file<>'1'");
        if(xbxData!=null) {
            Msg+="乳腺癌B超有未审核数据";
            return AjaxResult.error(Msg);
        }
        Record ydjData=Db.findFirst("SELECT * FROM rxaxx where save_file<>'1'");
        if(ydjData!=null) {
            Msg+="乳腺癌钼钯有未审核数据";
            return AjaxResult.error(Msg);
        }

        return AjaxResult.success(Msg);
    }

    }